package com.mcptt.config.body.location;

import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class LocationInfoBody {

  private static final String End_Mcptt_Tag = "</location-info>";
  public static final int Type_Request = 1;
  public static final int Type_Report = 2;
  public static final int Type_Configuration = 3;
  public static final int Type_Err = -1;
  public static final String PTT_ACTION       ="PTT_ACTION";

  public LocationInfoBody()
  {

  }


  public static int parseMsgType(String bodyStr) 
  {
    
    int index = bodyStr.indexOf(End_Mcptt_Tag);
    bodyStr= bodyStr.substring(0, index + End_Mcptt_Tag.length());
    bodyStr = bodyStr.trim();
  
    int returnCode = Type_Err;

    XmlPullParser parser = Xml.newPullParser();

    String funname;

    try {
      parser.setInput(new StringReader(bodyStr));
      int eventType = parser.getEventType();

      while (eventType != XmlPullParser.END_DOCUMENT) {

        funname = parser.getName();
        if (funname != null) {
          // 及时消息处理
          if (funname.equals("Request")) 
          {
            returnCode = Type_Request;
            break;
          } 
          else if (funname.equals("Report")) 
          {
            returnCode = Type_Report;
            break;
          }
          else if (funname.equals("Configuration")) {
            returnCode = Type_Configuration;
            break;
          }
        }

        eventType = parser.next();
      }

    } catch (Exception e) {
      e.printStackTrace();
      returnCode = Type_Err;
    }
    return returnCode;
  }
//	public LocationInfoBody()
//	{
//	}
	
	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">").append("\r\n");
		strBuff.append("<Report ReportID=\"xxx\" ReportType=\"Emergency\">").append("\r\n");
		strBuff.append("<TriggerId>xxx</TriggerId>").append("\r\n");
		
		strBuff.append("<CurrentLocation>").append("\r\n");
		strBuff.append("<CurrentCoordinate>").append("\r\n");
		
		strBuff.append("<longitude type=\"Normal\">").append("\r\n");
		strBuff.append("<three-bytes>16777215</three-bytes>").append("\r\n");
		strBuff.append("</longitude>").append("\r\n");
		
		strBuff.append("<latitude type=\"Normal\">").append("\r\n");
		strBuff.append("<three-bytes>16777215</three-bytes>").append("\r\n");
		strBuff.append("</latitude>").append("\r\n");
		
		strBuff.append("</CurrentCoordinate>").append("\r\n");
		strBuff.append("</CurrentLocation>").append("\r\n");
		strBuff.append("</Report>").append("\r\n");
		
		strBuff.append("</location-info>");
		
		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return strBuff.toString();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
